package com.tdtxdcxm.myparallelview.slice;

import com.tdtxdcxm.myparallelview.ResourceTable;
import com.tdtxdcxm.myparallelview.provider.PSProvider;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.webengine.WebAgent;
import ohos.agp.components.webengine.WebView;
import ohos.agp.utils.TextAlignment;
import ohos.bundle.AbilityInfo;


import java.util.ArrayList;

public class MainAbilitySlice extends AbilitySlice {
    private DirectionalLayout main_one,main_two;
    private TabList main_one_tablist;
    private TabList.Tab tab1,tab2,tab3,tab4,tab5,tab6,tab7,tab8,tab9;
    private PageSlider main_one_pageslider;

    private ArrayList<DirectionalLayout> pageslist = new ArrayList<>();

    private boolean islandscape = false;

    public void fillPagesList(){
        pageslist.clear();

        /**<********************************************加载已经的不同页面到pageslider中******************************************/
        DirectionalLayout directionalLayout = (DirectionalLayout) LayoutScatter.getInstance(this.getContext()).parse(ResourceTable.Layout_pageslider_page1,null,false);
        pageslist.add(directionalLayout);

        directionalLayout = (DirectionalLayout) LayoutScatter.getInstance(this.getContext()).parse(ResourceTable.Layout_pageslider_page2,null,false);
        pageslist.add(directionalLayout);
        /***********************************************加载已经的不同页面到pageslider中**************************************>*/

        /**<******************************************根据java代码创建页面到pageslider中****************************************/
        directionalLayout = new DirectionalLayout(this);
        Text text = new Text(this.getContext());
        text.setTextAlignment(TextAlignment.CENTER);
        text.setText("点我查看详细内容"+"\n"+"竖屏-跳Slice,横屏-平行视界");
        text.setTextSize(70);
        text.setMultipleLine(true);
        text.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if(islandscape == true){
                    //如果此时是横屏状态，则详情内容打开在“自制平行视界”中
                    main_two.setVisibility(Component.VISIBLE);
                    DirectionalLayout englishdetail_rootdl = (DirectionalLayout) LayoutScatter.getInstance(MainAbilitySlice.this.getContext()).parse(ResourceTable.Layout_english_detailpage,null,false);
                    Button englishdetail_backbut = (Button) englishdetail_rootdl.findComponentById(ResourceTable.Id_englishdetail_backbut);
                    WebView englishdetail_webview = (WebView) englishdetail_rootdl.findComponentById(ResourceTable.Id_englishdetail_webview);

                    englishdetail_backbut.setClickedListener(new Component.ClickedListener() {
                        @Override
                        public void onClick(Component component) {
                            main_two.removeComponentAt(0);
                            main_two.setVisibility(Component.HIDE);
                        }
                    });
                    main_two.addComponent(englishdetail_rootdl);
                    englishdetail_webview.getWebConfig().setJavaScriptPermit(true);
                    englishdetail_webview.setWebAgent(new WebAgent());
                    englishdetail_webview.load("https:www.csdn.net/");
                }
                else{
                    main_two.setVisibility(Component.HIDE);
                    present(new DetailSlice(), new Intent());
                }
            }
        });
        directionalLayout.addComponent(text);
        pageslist.add(directionalLayout);
        /*********************************************根据java代码创建页面到pageslider中**************************************>*/
    }

    public void initPageSlider(PageSlider pageslider){
        if(pageslider == null){
            return;
        }
        pageslider.setPageSwitchTime(50);
        pageslider.setSlidingPossible(true);
        pageslider.setReboundEffect(true);
        pageslider.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i, float v, int i1) {

            }

            @Override
            public void onPageSlideStateChanged(int i) {

            }

            @Override
            public void onPageChosen(int i) {
                main_one_tablist.selectTabAt(i);
            }
        });
        pageslider.setProvider(new PSProvider(pageslist));
    }

    public void initTabList(TabList tablist){
        if(tablist == null){
            return;
        }

        tablist.addTabSelectedListener(new TabList.TabSelectedListener() {
            @Override
            public void onSelected(TabList.Tab tab) {
                int i = tablist.getSelectedTabIndex();
                System.out.println("选中的TAB序号是："+i);
                main_one_pageslider.setCurrentPage(i);
            }

            @Override
            public void onUnselected(TabList.Tab tab) {
                return;
            }

            @Override
            public void onReselected(TabList.Tab tab) {
                return;
            }
        });

        tab1 = tablist.new Tab(this.getContext());
        tab1.setText("语文");
        tablist.addTab(tab1);

        tab2 = tablist.new Tab(this.getContext());
        tab2.setText("数学");
        tablist.addTab(tab2);

        tab3 = tablist.new Tab(this.getContext());
        tab3.setText("英语");
        tablist.addTab(tab3);

        tab4 = tablist.new Tab(this.getContext());
        tab4.setText("物理");
        tablist.addTab(tab4);

        tab5 = tablist.new Tab(this.getContext());
        tab5.setText("化学");
        tablist.addTab(tab5);

        tab6 = tablist.new Tab(this.getContext());
        tab6.setText("生物");
        tablist.addTab(tab6);

        tab7 = tablist.new Tab(this.getContext());
        tab7.setText("地理");
        tablist.addTab(tab7);

        tab8 = tablist.new Tab(this.getContext());
        tab8.setText("历史");
        tablist.addTab(tab8);

        tab9 = tablist.new Tab(this.getContext());
        tab9.setText("政治");
        tablist.addTab(tab9);

        tablist.selectTabAt(0);
    }

    public void initMASliceComponents(){
        main_one = (DirectionalLayout) findComponentById(ResourceTable.Id_main_one);
        main_two = (DirectionalLayout) findComponentById(ResourceTable.Id_main_two);
        main_one_tablist = (TabList) findComponentById(ResourceTable.Id_main_one_tablist);
        main_one_pageslider = (PageSlider) findComponentById(ResourceTable.Id_main_one_pageslider);
    }

    @Override
    protected void onOrientationChanged(AbilityInfo.DisplayOrientation displayOrientation) {
        System.out.println(displayOrientation);
        if(displayOrientation == AbilityInfo.DisplayOrientation.LANDSCAPE){
            islandscape = true;
        }
        else{
            islandscape = false;
            if(main_two.getComponentAt(0) != null){
                main_two.removeComponentAt(0);
            }
            main_two.setVisibility(Component.HIDE);
        }
    }

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        initMASliceComponents();

        initTabList(main_one_tablist);
        fillPagesList();
        initPageSlider(main_one_pageslider);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onInactive() {
        super.onInactive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onBackground() {
        super.onBackground();
    }

    @Override
    public void onStop() {
        super.onStop();
    }
}
